package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.HemaUtil;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;

import xtom.frame.util.XtomToastUtil;

/**
 * 找回密码第一步
 */
public class GetPassword0Activity extends BaseActivity {
	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private EditText usernameEditText;
	private TextView textView;
	private EditText codeEditText;
	private Button sendButton;
	private TextView secondTextView;

	private String username;
	private TimeThread timeThread;
    private String keytype = "1";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_getpassword0);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onDestroy() {
		if (timeThread != null)
			timeThread.cancel();
		super.onDestroy();
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_VERIFY:
			showProgressDialog("正在验证手机号");
			break;
		case CODE_GET:
			showProgressDialog("正在获取验证码");
			break;
		case CODE_VERIFY:
			showProgressDialog("正在验证随机码");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_VERIFY:
		case CODE_GET:
		case CODE_VERIFY:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_VERIFY:
			username = netTask.getParams().get("username");
			getNetWorker().codeGet(username);
			break;
		case CODE_GET:
			textView.setText("验证码已发送到 " + HemaUtil.hide(username, "1"));
			textView.setVisibility(View.VISIBLE);
			timeThread = new TimeThread(new TimeHandler(this));
			timeThread.start();
			break;
		case CODE_VERIFY:
			@SuppressWarnings("unchecked")
			HemaArrayResult<String> sResult = (HemaArrayResult<String>) baseResult;
			String tempToken = sResult.getObjects().get(0);
			Intent it = new Intent(mContext, GetPassword1Activity.class);
			it.putExtra("username", username);
			it.putExtra("tempToken", tempToken);
            it.putExtra("keytype", keytype);
			startActivity(it);
            finish();
			break;
		default:
			break;
		}

	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_VERIFY:
			showTextDialog(baseResult.getMsg());
			break;
		case CODE_GET:
			showTextDialog(baseResult.getMsg());
			break;
		case CODE_VERIFY:
			showTextDialog(baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case CLIENT_VERIFY:
			showTextDialog("验证手机号失败");
			break;
		case CODE_GET:
			showTextDialog("获取验证码失败");
			break;
		case CODE_VERIFY:
			showTextDialog("验证随机码失败");
			break;
		default:
			break;
		}
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);

		usernameEditText = (EditText) findViewById(R.id.username);
		textView = (TextView) findViewById(R.id.textview);
		codeEditText = (EditText) findViewById(R.id.code);
		secondTextView = (TextView) findViewById(R.id.second);
		sendButton = (Button) findViewById(R.id.sendcode);
	}

	@Override
	protected void getExras() {
        keytype = mIntent.getStringExtra("keytype");
	}

	@Override
	protected void setListener() {
		titleText.setText("找回密码");
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		sendButton.setOnClickListener(new SendButtonListener());
		codeEditText.addTextChangedListener(new OnTextChangeListener());
		titleRight.setText("下一步");
		titleRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isNull(username)) {
					XtomToastUtil.showShortToast(mContext, "请先验证手机号");
					return;
				}
				String code = codeEditText.getText().toString();
				if (isNull(code)) {
					XtomToastUtil.showShortToast(mContext, "请输入验证码");
					return;
				}
				getNetWorker().codeVerify(username, code);
			}
		});
	}

	private class SendButtonListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			String username = usernameEditText.getText().toString();
			if (isNull(username)) {
				showTextDialog("请输入手机号");
				return;
			}
			String mobile = "^[1][3-8]+\\d{9}";
			if (!username.matches(mobile)) {
				showTextDialog("您输入的手机号不正确");
				return;
			}

			getNetWorker().clientVerify(username);
		}

	}

	private class TimeThread extends Thread {
		private int curr;

		private TimeHandler timeHandler;

		public TimeThread(TimeHandler timeHandler) {
			this.timeHandler = timeHandler;
		}

		void cancel() {
			curr = 0;
		}

		@Override
		public void run() {
			curr = 60;
			while (curr > 0) {
				timeHandler.sendEmptyMessage(curr);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// ignore
				}
				curr--;
			}
			timeHandler.sendEmptyMessage(-1);
		}
	}

	private static class TimeHandler extends Handler {
		GetPassword0Activity activity;

		public TimeHandler(GetPassword0Activity activity) {
			this.activity = activity;
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case -1:
				activity.sendButton.setText("重新发送");
				activity.sendButton.setVisibility(View.VISIBLE);
				break;
			default:
				activity.sendButton.setVisibility(View.GONE);
				activity.secondTextView.setText("" + msg.what);
				break;
			}
		}
	}

	private class OnTextChangeListener implements TextWatcher {

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			checkNextable();
		}
	}

	private void checkNextable() {
		String code = codeEditText.getText().toString();
		// boolean c = code.matches("\\d{6}$");
		boolean c = !isNull(code);
		if (c) {
			titleRight.setEnabled(true);
		} else {
			titleRight.setEnabled(false);
		}
	}
}
